<?php

namespace App\Service\Common;

use Kuafu\File\FileUtil;
use OSS\Core\OssException;

class OssService
{
    /**
     * 上传服务器文件到OSS
     * @param string $filepath
     * @param string|null $suffix 指定文件后缀，例如：.jpg或者jpg
     * @param string|null $objectName
     * @return array
     * @throws OssException
     */
    public static function uploadFile(string $filepath, string $suffix = null, string $objectName = null): array
    {
        return FileUtil::uploadFile($filepath, $suffix, $objectName);
    }

    /**
     * OSS 直连配置
     *
     * @return array
     */
    public static function getUploadAccess()
    {
        return array_merge([
            'domain' => FileUtil::domain()
        ], FileUtil::getWebUploadAccess());
    }

    /**
     * 获取 STS 用户信息
     *
     * @return array
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public static function getSTSAccessInfo(): array
    {
        $accessInfo = FileUtil::genAliyunSTSAccessKey();

        $credentials = [
            'securityToken'   => $accessInfo['Credentials']['SecurityToken'],
            'accessKeyId'     => $accessInfo['Credentials']['AccessKeyId'],
            'accessKeySecret' => $accessInfo['Credentials']['AccessKeySecret'],
            'expiration'      => 1200 + time(),
        ];

        return [
            'config'      => [
                'domain'   => FileUtil::domain(),
                'bucket'   => FileUtil::bucket(),
                'endpoint' => str_replace("-internal", "", FileUtil::endpoint()),
                'region'   => FileUtil::region(),
                'path'     => '' // 上传地址
            ],
            'credentials' => $credentials
        ];
    }


}
